import { Injectable } from '@nestjs/common';
import { DiscussModel } from 'src/Scheam/discuss.schema';
@Injectable()
export class DiscussService {

    async create(data) {

        try {
            return await DiscussModel.create({ ...data })
        } catch (error) {
            return false
        }
    }
    async getcontent(id: string) {
        try {
            return await DiscussModel.aggregate([
                {
                    $match: {
                        food_id: id
                    }
                },
                {
                    $addFields: {
                        userid: { $toObjectId: '$user_id' },
                    }
                },
                {
                    $sort: { "publish_time": -1 }
                },
                {
                    $lookup: {
                        from: 'user',
                        localField: 'userid',
                        foreignField: '_id',
                        as: 'user'
                    }
                },
                {
                    $unwind: "$user"
                },
                {
                    $project: {
                        content: 1,
                        publish_time: 1,
                        'user.avatar': 1,
                        'user.username': 1
                    }
                }
            ])
        } catch (error) {
            return false
        }
    }

    async getDiscussList(skip = 0, Order, word = '') {
        try {
            if (Order == 'desc') {
                if (!word) {
                    return {
                        data: await DiscussModel.aggregate([
                            {
                                $addFields: {
                                    userid: { $toObjectId: '$user_id' },
                                }
                            },
                            {
                                $sort: { "publish_time": -1 }
                            },
                            {
                                $skip: skip
                            },
                            {
                                $limit: 7
                            },
                            {
                                $sort: { "publish_time": 1 }
                            },
                            {
                                $lookup: {
                                    from: 'user',
                                    localField: 'userid',
                                    foreignField: '_id',
                                    as: 'user'
                                }
                            },
                            {
                                $unwind: "$user"
                            },
                            {
                                $project: {
                                    content: 1,
                                    publish_time: 1,
                                    'user.avatar': 1,
                                    'user.username': 1,
                                    'user.constellation': 1,
                                    'user.sex': 1.,
                                    'user.brithday': 1,

                                }
                            }
                        ]),
                        count: await DiscussModel.aggregate([
                            {
                                $match: {
                                    content: { $regex: word, $options: 'i' }
                                }
                            },
                            {
                                $count: "total"
                            }
                        ])
                    }
                } else {
                    return {
                        data: await DiscussModel.aggregate([
                            {
                                $match: {
                                    content: { $regex: word, $options: 'i' }
                                }
                            },
                            {
                                $addFields: {
                                    userid: { $toObjectId: '$user_id' },
                                }
                            },
                            {
                                $sort: { "publish_time": -1 }
                            },
                            {
                                $skip: skip
                            },
                            {
                                $limit: 7
                            },
                            {
                                $sort: { "publish_time": 1 }
                            },
                            {
                                $lookup: {
                                    from: 'user',
                                    localField: 'userid',
                                    foreignField: '_id',
                                    as: 'user'
                                }
                            },
                            {
                                $unwind: "$user"
                            },
                            {
                                $project: {
                                    content: 1,
                                    publish_time: 1,
                                    'user.avatar': 1,
                                    'user.username': 1,
                                    'user.constellation': 1,
                                    'user.sex': 1.,
                                    'user.brithday': 1,

                                }
                            }
                        ]),
                        count: await DiscussModel.aggregate([
                            {
                                $match: {
                                    content: { $regex: word, $options: 'i' }
                                }
                            },
                            {
                                $count: "total"
                            }
                        ])
                    }
                }
            } else {
                if (!word) {
                    return {
                        data: await DiscussModel.aggregate([
                            {
                                $addFields: {
                                    userid: { $toObjectId: '$user_id' },
                                }
                            },
                            {
                                $sort: { "publish_time": -1 }
                            },
                            {
                                $skip: skip
                            },
                            {
                                $limit: 7
                            },
                            {
                                $sort: { "publish_time": -1 }
                            },
                            {
                                $lookup: {
                                    from: 'user',
                                    localField: 'userid',
                                    foreignField: '_id',
                                    as: 'user'
                                }
                            },
                            {
                                $unwind: "$user"
                            },
                            {
                                $project: {
                                    content: 1,
                                    publish_time: 1,
                                    'user.avatar': 1,
                                    'user.username': 1,
                                    'user.constellation': 1,
                                    'user.sex': 1.,
                                    'user.brithday': 1,

                                }
                            }
                        ]),
                        count: await DiscussModel.aggregate([
                            {
                                $match: {
                                    content: { $regex: word, $options: 'i' }
                                }
                            },
                            {
                                $count: "total"
                            }
                        ])
                    }
                } else {
                    return {
                        data: await DiscussModel.aggregate([
                            {
                                $match: {
                                    content: { $regex: word, $options: 'i' }
                                }
                            },
                            {
                                $addFields: {
                                    userid: { $toObjectId: '$user_id' },
                                }
                            },
                            {
                                $sort: { "publish_time": -1 }
                            },
                            {
                                $skip: skip
                            },
                            {
                                $limit: 7
                            },
                            {
                                $sort: { "publish_time": -1 }
                            },
                            {
                                $lookup: {
                                    from: 'user',
                                    localField: 'userid',
                                    foreignField: '_id',
                                    as: 'user'
                                }
                            },
                            {
                                $unwind: "$user"
                            },
                            {
                                $project: {
                                    content: 1,
                                    publish_time: 1,
                                    'user.avatar': 1,
                                    'user.username': 1,
                                    'user.constellation': 1,
                                    'user.sex': 1.,
                                    'user.brithday': 1,

                                }
                            }
                        ]),
                        count: await DiscussModel.aggregate([
                            {
                                $match: {
                                    content: { $regex: word, $options: 'i' }
                                }
                            },
                            {
                                $count: "total"
                            }
                        ])
                    }
                }

            }
        } catch (error) {
            return false
        }
    }

    async deleteOne(id: string) {
        try {
            return await DiscussModel.deleteOne({ _id: id })
        } catch (error) {
            return false
        }
    }
}
